<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form
              ref="userForm"
              label-width="120px"
              style="margin-left: 120px; margin-top: 30px"
              :model="userInfo"
              :rules="rules"
            >
              <el-form-item label="姓名:" prop="username">
                <el-input style="width: 300px" v-model="userInfo.username" />
              </el-form-item>
              <el-form-item label="密码:" prop="password2">
                <el-input
                  style="width: 300px"
                  type="password"
                  v-model="userInfo.password2"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUser">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <!-- 个人详情 -->
          <el-tab-pane label="个人详情">
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人信息">
                <router-link :to="`/employees/print/${userId}?type=personal`">
                  <i class="el-icon-printer"></i>
                </router-link>
              </el-tooltip>
            </el-row>
            <!-- 这里的is 必须要变量 不然动态组件就无意义 -->
            <!-- component 官方提供 动态展示组件 -->
            <component :is="UserComponent"></component>
          </el-tab-pane>
          <!-- 岗位信息 -->
          <el-tab-pane label="岗位信息">
            <el-row type="flex" justify="end">
              <el-tooltip content="打印岗位信息">
                <router-link :to="`/employees/print/${userId}?type=job`">
                  <i class="el-icon-printer"></i>
                </router-link>
              </el-tooltip>
            </el-row>
            <component :is="JobComponent"></component>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailById } from "@/api/user";
import { saveUserDetailByIdAPI } from "@/api/employees";
import userInfo from "./components/user-info.vue";
import jobInfo from "./components/job-info.vue";
export default {
  components: {
    userInfo,
    jobInfo,
  },
  data() {
    return {
      // 动态路由组件变量名
      UserComponent: "userInfo",
      JobComponent: "jobInfo",
      // 路由传参传递过来 当前点击查看用户的id
      userId: this.$route.params.id,
      userInfo: {
        //专门存放基本信息
        username: "",
        password2: "",
      },
      rules: {
        username: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
        ],
        password2: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 6, max: 9, message: "密码长度6-9位", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    async saveUser() {
      try {
        await this.$refs.userForm.validate();
        const { userInfo } = this;
        userInfo.passwrod = userInfo.password2;
        await saveUserDetailByIdAPI({
          ...userInfo,
          password: userInfo.password2,
        });
        this.$message.success("保存成功");
      } catch (error) {
        this.$message.error(error || "出错了请稍后重试");
      }
    },
    async getUserDetailById() {
      let password2 = this.userInfo.password2;
      const result = await getUserDetailById(this.userId);
      this.userInfo = { ...result, password2 };
    },
  },
  created() {
    this.getUserDetailById();
  },
};
</script>

<style>
</style>